<?php


/**
 * Notes:
 * PROJECT_NAME : ERP v1.8 重构erp
 * User: 夏鱼儿<397072174@qq.com>
 * Date: 2024/8/9
 * Time: 15:59
 */

namespace app\command\company\data_change;

use app\admin\model\AdminUser;
use app\admin\model\CompanyUser;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use app\company\model\CompanyDividend;
use app\admin\model\Company;

class Dividend extends  Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('data:change:6')->setDescription('添加公司分红比例信息');
    }


    protected function execute(Input $input, Output $output)
    {
        $output->writeln('添加公司分红比例信息/companyOrderStorage添加字段active_time 初始化1111');
        $model = new Company();
        $model->startTrans();
        try {
            $model->select()->each(function ($item) {
                //已有公司添加admin  用户
                $user = CompanyUser::gs($item->id)->find();
                $adminUser = AdminUser::create([
                    'user_name' => $user->user_name,
                    'password'  => md5(rand(10000, 99999) . rand(10000, 99999)),
                    'real_name' => $user->real_name,
                    'mobile'    => $user->mobile,
                    'identity'  => 5
                ]);
                //添加分红设置
                CompanyDividend::create(['company_id' => $item->id,'partner_id'=> $adminUser->id]);
            });
            //companyOrderStorage添加字段active_time 初始化
            \app\mini\model\CompanyOrderStorage::where('active_time',0)->withTrashed()->select()->each(function ($item) use ($output) {
                $active_time = strtotime($item->create_time);
                \app\mini\model\CompanyOrderStorage::where('id',$item->id)->withTrashed()->update(['active_time'=>$active_time]);
            });
            $model->commit();
            $output->writeln('添加公司分红比例信息/companyOrderStorage添加字段active_time 初始化 成功');
        } catch (\Exception $e) {
            $model->rollback();
            $output->writeln('添加公司分红比例信息/companyOrderStorage添加字段active_time 初始化失败' . $e->getMessage() . $e->getLine() . $e->getFile());
        }
    }
}